package com.hehe.httpurlconnectiontest2;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

/**
 * Created by lenove on 2017/11/11.
 * JSON数据解析类
 * 这个类负责解析从网络上获得的JSON数据
 */

public class JSONData {

    //对获取的ApiKey数据的解析，返回用户的apikey
    public static String getApiKey(String json){

        if (json == "500"){//服务器异常直接返回响应码
            return json;
        }

        String errcode=null;
        String errmsg=null ;
        String apikey=null ;

        try {

            JSONArray jsonArray = new JSONArray(json);
            JSONObject jsonObject = (JSONObject)jsonArray.get(0);
            errcode = jsonObject.getString("errcode");
            errmsg = jsonObject.getString("errmsg");
            apikey = jsonObject.getString("apikey");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (errcode != null) {
            switch (errcode){
                case "0"://没有发生错误
                    return apikey;
                case "10000"://用户名或密码错误
                    Log.e("getApiKey:errcode=",errcode+"用户名或密码错误");
                    return errcode;
                case "10001"://请求方式不正确
                    Log.e("getApiKey:errcode=",errcode+"请求方式不正确");
                    return errcode;
                default:
                    return errcode;
            }
        }else {
            Log.e("getApiKey:errcode=","null");
            return "null";
        }
    }

    //对返回的设备列表的JSON解析
    public static  LinkedList<YeelinkDecives> getDevicesList(String json){

        String id ;
        String title;
        String about;
        LinkedList<YeelinkDecives> deviceslist = new LinkedList<YeelinkDecives>();
        try {
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0;i<jsonArray.length();i++){
                JSONObject jsonObject = (JSONObject)jsonArray.get(i);
                id = jsonObject.getString("id");
                title = jsonObject.getString("title");
                about = jsonObject.getString("about");
                YeelinkDecives device = new YeelinkDecives(id,title,about,null);
                deviceslist.add(device);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return deviceslist;
    }

    //对传感器列表JSON数据的解析
    public static  LinkedList<YeelinkSensor> getSensorsList(String json){
        String id;
        String title;
        String about;
        String type;
        String last_update ;
        String last_data;
        String last_data_gen;
        LinkedList<YeelinkSensor> sensorlist = new LinkedList<YeelinkSensor>();

        try {
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0;i<jsonArray.length();i++){
                JSONObject jsonObject = (JSONObject)jsonArray.get(i);
                id = jsonObject.getString("id");
                title = jsonObject.getString("title");
                about = jsonObject.getString("about");
                YeelinkSensor sensor = new YeelinkSensor(id,title,about,null,null,null);
                sensorlist.add(sensor);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return sensorlist;
    }

    //对数据点JSON数据的解析
    public static  YeelinkDataPoint getDataPoint(String json){
        String timestamp;
        String value;
        YeelinkDataPoint dataPoint = null;

//      JSONArray jsonArray = new JSONArray(json);
//      出现错误
//      org.json.JSONException: Value {"timestamp":"2017-07-23T16:53:26","value":2} of type org.json.JSONObject cannot be converted to JSONArray
//        原因是，输入的不是数组数据。
//        [{},{}] 这样的才是数组。
        try {
            JSONObject jsonObject = new JSONObject(json);
            timestamp = jsonObject.getString("timestamp");
            value = jsonObject.getString("value");
            dataPoint = new YeelinkDataPoint(timestamp,value);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return dataPoint;
    }

    //对传感器详细信息JSON数据解析
    public static  void getSensorInfo(YeelinkSensor yeelinkSensor,String json){
        String unit_name;
        String unit_symbol;
        try {
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0;i<jsonArray.length();i++){
                JSONObject jsonObject = (JSONObject)jsonArray.get(i);
                unit_name = jsonObject.getString("unit_name");
                unit_symbol = jsonObject.getString("unit_symbol");
                yeelinkSensor.setUnit_name(unit_name);
                yeelinkSensor.setUnit_symbol(unit_symbol);
                Log.i("sensor info",yeelinkSensor.getUnit_symbol());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}
